package com.niit.blog.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.niit.blog.entity.Dto.CommentControDto;
import com.niit.blog.entity.MyEnum.IsDeletedFlagEnum;
import com.niit.blog.entity.domain.TbBlogComment;
import com.niit.blog.entity.page.PageReq;
import com.niit.blog.entity.page.PageReqreq;
import com.niit.blog.entity.req.CommentReq;
import com.niit.blog.entity.req.RemoveShareCommentReq;
import com.niit.blog.entity.req.SaveBlogCommentReplyReq;
import com.niit.blog.mapper.TbBlogCommentMapper;
import com.niit.blog.service.TbBlogCommentService;
import com.niit.blog.util.XueHuaUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author 18512
* @description 针对表【tb_blog_comment】的数据库操作Service实现
* @createDate 2024-06-25 20:33:55
*/
@Service
@Slf4j
public class TbBlogCommentServiceImpl extends ServiceImpl<TbBlogCommentMapper, TbBlogComment>
    implements TbBlogCommentService {
    @Resource
    private TbBlogCommentMapper commentMapper;




    @Override
    public Integer saveBlogComment(TbBlogComment data) {
        log.info("TbBlogComment准备更新的数据为:{}",data);


        int i = commentMapper.insertData(data);


        return i;
    }

    @Override
    public Boolean removeComment(RemoveShareCommentReq req) {
        return null;
    }

    @Override
    public PageInfo<CommentControDto> selectByPage(PageReqreq pageReq, Integer pageNo, Integer pageSize) {

        if (pageReq == null || isPageReqEmpty(pageReq)) {
            // 如果 PageReq 为空或关键字段为空，则执行普通的分页查询
            return selectByCustomPage(pageNo, pageSize);
        } else {
            if (StringUtils.isEmpty(pageReq.getReplyType())) {
                pageReq.setReplyType(null); // 将空字符串转换为 null
            }
            if (StringUtils.isEmpty(pageReq.getReplyUser())) {
                pageReq.setReplyUser(null); // 将空字符串转换为 null
            }

            // 执行传入的分页查询
            PageInfo<CommentControDto> objectPageInfo = PageHelper.startPage(pageNo, pageSize)
                    .doSelectPageInfo(() -> this.commentMapper.getPageList(pageReq));

            // 对返回的数据列表进行截取，保留指定数量的数据条目
            List<CommentControDto> resultList = objectPageInfo.getList();
            if (resultList.size() > pageSize) {
                resultList = resultList.subList(0, pageSize);
                objectPageInfo.setList(resultList);
            }
            return objectPageInfo;
        }

    }

    @Override
    public TbBlogComment prepareData(CommentReq commentReq) {
        Date date = new Date();
        TbBlogComment tbBlogComment = new TbBlogComment();
        XueHuaUtil xueHuaUtil = new XueHuaUtil(1,1,0);

        tbBlogComment.setId(xueHuaUtil.nextId());
        tbBlogComment.setBlogId(commentReq.getBlogId());
        tbBlogComment.setContent(commentReq.getContent());
        tbBlogComment.setReplyType(commentReq.getReplyType());
        tbBlogComment.setToUser(commentReq.getToUser());
        tbBlogComment.setToUserauthor(commentReq.getToUserauthor());
        tbBlogComment.setToId(commentReq.getToId());
        tbBlogComment.setReplyUser(commentReq.getReplyUser());
        tbBlogComment.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        tbBlogComment.setParentId(commentReq.getParentId());
        tbBlogComment.setReplyAuthor(commentReq.getReplyAuthor());
        tbBlogComment.setCreateBy("yuanzi");
        tbBlogComment.setCreateTime(date);
        tbBlogComment.setUpdateBy("yuanzi");

        return tbBlogComment;
    }

    // 检查 PageReq 是否为空
    private boolean isPageReqEmpty(PageReqreq pageReq) {
        return pageReq.getBlogId() == null
                && isEmpty(pageReq.getReplyType())
                && pageReq.getToId() == null
                && pageReq.getReplyId() == null
                && isEmpty(pageReq.getReplyUser())
                && pageReq.getIsDeleted() == null;
    }

    private boolean isEmpty(String str) {
        return str == null || str.trim().isEmpty();
    }
    // 构建查询条件
    private QueryWrapper<TbBlogComment> buildQueryWrapper(PageReqreq pageReq) {
        QueryWrapper<TbBlogComment> queryWrapper = new QueryWrapper<>();
        if (pageReq.getBlogId() != null) {
            queryWrapper.eq("blog_id", pageReq.getBlogId());
        }
        if (pageReq.getReplyType() != null) {
            queryWrapper.eq("reply_type", pageReq.getReplyType());
        }
        if (pageReq.getToId() != null) {
            queryWrapper.eq("to_id", pageReq.getToId());
        }

        if (pageReq.getReplyId() != null) {
            queryWrapper.eq("reply_id", pageReq.getReplyId());
        }
        if (pageReq.getReplyId() != null) {
            queryWrapper.eq("reply_user", pageReq.getReplyId());
        }

        if (pageReq.getReplyId() != null) {
            queryWrapper.eq("is_deleted", pageReq.getReplyId());
        }

        return queryWrapper;
    }

    // 单纯的分页查询方法，根据具体需求实现
    private PageInfo<CommentControDto> selectByCustomPage(Integer pageNo, Integer pageSize) {
        List<TbBlogComment> totalList = commentMapper.selectAll(); // 获取总记录数

        // 计算分页查询的起始索引
        int offset = (pageNo - 1) * pageSize;

        // 执行分页查询
        List<CommentControDto> list = commentMapper.selectMyByPage(offset, pageSize);

        // 构造 PageInfo 对象
        PageInfo<CommentControDto> pageInfo = new PageInfo<>(list);
        pageInfo.setPageNum(pageNo); // 设置当前页码
        pageInfo.setPageSize(pageSize); // 设置每页记录数
        pageInfo.setTotal(totalList.size()); // 设置总记录数
        pageInfo.setPages((int) Math.ceil((double) totalList.size() / pageSize)); // 设置总页数
  return pageInfo;
    }

}




